package com.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.*;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Resource
    private ShoppingCartMapper shoppingCartMapper;
    @Resource
    private DishMapper dishMapper;
    @Resource
    private SetmealMapper setmealMapper;
    @Override
    public void addShoppingCartDTO(ShoppingCartDTO dto) {
        Long id = BaseContext.getCurrentId();
        QueryWrapper<ShoppingCart> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",id );
        if(dto.getDishId()!=null){
            wrapper.eq("dish_id",dto.getDishId());
        } else if (dto.getSetmealId() != null) {
            wrapper.eq("setmeal_id",dto.getSetmealId());
        }
        if(dto.getDishFlavor()!=null&& !Objects.equals(dto.getDishFlavor(), ""))
            wrapper.eq("dish_flavor",dto.getDishFlavor());
        ShoppingCart shoppingCart = shoppingCartMapper.selectOne(wrapper);
        //如果之前存在相同的商品就加一
        if (shoppingCart!=null){
            shoppingCart.setNumber(shoppingCart.getNumber()+1);
            shoppingCartMapper.updateById(shoppingCart);
        }else{
            //不存在就插入一条新的数据

            shoppingCart=new ShoppingCart();
            shoppingCart.setDishFlavor(dto.getDishFlavor());
            shoppingCart.setNumber(1);
            shoppingCart.setUserId(id);
            shoppingCart.setCreateTime(LocalDateTime.now());
            //本次添加的是菜品
            if (dto.getDishId()!=null){
                Dish dish = dishMapper.selectById(dto.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setDishId(dto.getDishId());
            }else{
                //添加的是套餐
                Setmeal setmeal = setmealMapper.selectById(dto.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setSetmealId(setmeal.getId());
            }
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    @Override
    public List<ShoppingCart> getShoppingCart() {
        Long id = BaseContext.getCurrentId();
        QueryWrapper<ShoppingCart> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",id);
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectList(wrapper);
        List<Long> dishIds = new ArrayList<>(shoppingCarts.size());
        List<Long> setmealIds = new ArrayList<>(shoppingCarts.size());

        for (int i = 0; i < shoppingCarts.size(); i++) {
            if (shoppingCarts.get(i).getDishId()!=null)
                dishIds.add(shoppingCarts.get(i).getDishId());
            else
                setmealIds.add(shoppingCarts.get(i).getSetmealId());
        }
        List<Dish> dishes= Collections.emptyList();
        if (dishIds.size()>0)
            dishes = dishMapper.selectByIds(dishIds);
        List<Setmeal> setmeals=Collections.emptyList();
        if(setmealIds.size()>0)
            setmeals = setmealMapper.selectByIds(setmealIds);

        for (int i = 0; i < shoppingCarts.size(); i++) {
            int I = i;
            ShoppingCart shoppingCart = shoppingCarts.get(i);
            if (shoppingCart.getDishId()!=null) {
                Optional<Dish> any = dishes.stream().filter(v -> v.getId().equals(shoppingCarts.get(I).getDishId())).findAny();
                if (any.isPresent())
                {
                    Dish dish = any.get();
                    long price = dish.getPrice().longValue();
                    Integer number = shoppingCart.getNumber();
                    shoppingCart.setAmount(BigDecimal.valueOf(price*number));
                    shoppingCart.setImage(dish.getImage());
                }
            }
            else {
                Optional<Setmeal> any = setmeals.stream().filter(v -> v.getId().equals(shoppingCarts.get(I).getSetmealId())).findAny();
                if (any.isPresent())
                {
                    Setmeal setmeal = any.get();
                    long price = setmeal.getPrice().longValue();
                    Integer number = shoppingCart.getNumber();
                    shoppingCart.setImage(setmeal.getImage());
                    shoppingCart.setAmount(BigDecimal.valueOf(price*number));
                }
            }
        }
        return shoppingCarts;
    }

    @Override
    public void cleanShoppingCart() {
        Long id = BaseContext.getCurrentId();
        QueryWrapper<ShoppingCart> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",id);
        shoppingCartMapper.delete(wrapper);
    }

}
